package com.ninthgroup.service.Impl;

import com.ninthgroup.entity.MessageBooking;
import com.ninthgroup.mapper.MessageBookingMapper;
import com.ninthgroup.service.MessageBookingService;
import com.ninthgroup.util.DateFormatUtil;
import com.ninthgroup.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author wch
 * @version 1.0
 * @date 2022/10/14 19:26
 */
@Service
public class MessageBookingServiceImpl implements MessageBookingService {
    @Autowired
    private MessageBookingMapper messageBookingMapper;

    //查询所有预约信息
    @Override
    public Result selectAll() {
        List<MessageBooking> messageBookings = messageBookingMapper.selectAll();
        System.out.println("messageBookings = " + messageBookings);
        return new Result(200, "查询成功", messageBookings);
    }

    //预约用户查询
    @Override
    public Result insertAdd(MessageBooking messageBooking) {
        messageBookingMapper.insertSelective(messageBooking);
        return new Result(200, "添加成功");
    }

    @Override
    public Result delecte(Integer mId) {
        messageBookingMapper.deleteByPrimaryKey(mId);
        return new Result(200, "删除成功");
    }

    /**
     * 预约修改
     *
     * @param messageBooking
     * @return
     */
    @Override
    public Result update(MessageBooking messageBooking) {
        messageBookingMapper.updateByPrimaryKey(messageBooking);
        return new Result(200, "修改成功");
    }

    @Override
    public Result selectbing() {
        List<MessageBooking> selectbing = messageBookingMapper.selectbing();
        System.out.println("selectbing = " + selectbing);
        return new Result(200, "查询成功", selectbing);
    }

    @Override
    public Result saveDidAndStatus(Integer mId, Integer dId) {

        MessageBooking messageBooking = messageBookingMapper.selectByPrimaryKey(mId);
        messageBooking.setmYstatus(dId);
        messageBookingMapper.updateByPrimaryKeySelective(messageBooking);
        return new Result(200, "存入成功");
    }


    /**
     * 按照性别查找数量
     *
     * @return
     */
    @Override
    public Result selectBysex() {
        int man = messageBookingMapper.selectSexMan();
        int women = messageBookingMapper.selectSexWomen();
        List<Integer> sex = new ArrayList<Integer>();
        sex.add(man);
        sex.add(women);
        return new Result(200, "查询成功", sex);
    }

    /**
     * 按照年龄查找数量
     *
     * @return
     */
    @Override
    public Result selectByAge() {
        int i = messageBookingMapper.selectByAge0_20();
        int i1 = messageBookingMapper.selectByAge20_40();
        int i2 = messageBookingMapper.selectByAge40_60();
        int i3 = messageBookingMapper.selectByAge60();
        List<Integer> age = new ArrayList<Integer>();
        age.add(i);
        age.add(i1);
        age.add(i2);
        age.add(i3);
        return new Result(200, "查询成功", age);
    }

    @Override
    public Result selectByAllMoney() {

        List<MessageBooking> a = messageBookingMapper.selectAll();

        //分离到四个季度的对象集合
        //根据数据库的订单支付日期过滤然后叠加钱数
        List<MessageBooking> collect1 = a.stream().filter(item ->
        {
            int month = DateFormatUtil.dateToLocalDate(item.getmDate()).getMonth().getValue();
            if (month >= 1 && month <= 3) {
                return true;
            } else {
                return false;
            }
        }).collect(Collectors.toList());

        //第一季度的总钱数
        Double one = collect1.stream().collect(Collectors.summingDouble(item -> item.getmPrice().doubleValue()));

        List<MessageBooking> collect2 = a.stream().filter(item ->
        {
            int month = DateFormatUtil.dateToLocalDate(item.getmDate()).getMonth().getValue();
            if (month >= 4 && month <= 6) {
                return true;
            } else {
                return false;
            }
        }).collect(Collectors.toList());
        //第二季度的总钱数
        Double two = collect2.stream().collect(Collectors.summingDouble(item -> item.getmPrice().doubleValue()));

        List<MessageBooking> collect3 = a.stream().filter(item ->
        {
            int month = DateFormatUtil.dateToLocalDate(item.getmDate()).getMonth().getValue();
            if (month >= 7 && month <= 9) {
                return true;
            } else {
                return false;
            }
        }).collect(Collectors.toList());
        //第一季度的总钱数
        Double three = collect3.stream().collect(Collectors.summingDouble(item -> item.getmPrice().doubleValue()));

        List<MessageBooking> collect4 = a.stream().filter(item ->
        {
            int month = DateFormatUtil.dateToLocalDate(item.getmDate()).getMonth().getValue();
            if (month >= 10 && month <= 12) {
                return true;
            } else {
                return false;
            }
        }).collect(Collectors.toList());
        //第四季度的总钱数
        Double four = collect4.stream().collect(Collectors.summingDouble(item -> item.getmPrice().doubleValue()));

        //各季度的流水数据
        List<Double> b=new ArrayList<>();
        b.add(one);
        b.add(two);
        b.add(three);
        b.add(four);

        return new Result(200,"医院各季度流水",b);




    }


}
